//
//  YMHeartAnimation.m
//  bezierPath
//
//  Created by Andrew on 2018/7/25.
//  Copyright © 2018年 Andrew. All rights reserved.
//  心形动画

#import "YMHeartAnimation.h"

@implementation YMHeartAnimation

- (void)drawRect:(CGRect)rect {
    // Drawing code
    // 初始化UIBezierPath
    UIBezierPath *path = [UIBezierPath bezierPath];
    // 首先设置一个起始点
    CGPoint startPoint = CGPointMake(rect.size.width/2, 120);
    // 以起始点为路径的起点
    [path moveToPoint:startPoint];
    // 设置一个终点
    CGPoint endPoint = CGPointMake(rect.size.width/2, rect.size.height-40);
    // 设置第一个控制点
    CGPoint controlPoint1 = CGPointMake(100, 20);
    // 设置第二个控制点
    CGPoint controlPoint2 = CGPointMake(0, 180);
    // 添加三次贝塞尔曲线
    [path addCurveToPoint:endPoint controlPoint1:controlPoint1 controlPoint2:controlPoint2];
    // 设置另一个起始点
    [path moveToPoint:endPoint];
    // 设置第三个控制点
    CGPoint controlPoint3 = CGPointMake(rect.size.width-100, 20);
    // 设置第四个控制点
    CGPoint controlPoint4 = CGPointMake(rect.size.width, 180);
    // 添加三次贝塞尔曲线
    [path addCurveToPoint:startPoint controlPoint1:controlPoint4 controlPoint2:controlPoint3];
    // 设置线宽
    path.lineWidth = 3;
    // 设置线断面类型
    path.lineCapStyle = kCGLineCapRound;
    // 设置连接类型
    path.lineJoinStyle = kCGLineJoinRound;
    // 设置画笔颜色
    [[UIColor redColor] set];
    [path stroke];
    
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    //在动画设置一些变量
    pathAnimation.calculationMode = kCAAnimationPaced;
    //我们希望动画持续
    //如果我们动画从左到右的东西——我们想要呆在新位置,
    //然后我们需要这些参数
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.duration = 10;//完成动画的时间
    //让循环连续演示
    pathAnimation.repeatCount = 10;
    //设置的路径动画
//    CGMutablePathRef curvedPath = CGPathCreateMutable();
//    CGPathMoveToPoint(curvedPath, NULL, 10, 10);//起始位置
//    CGPathAddQuadCurveToPoint(curvedPath, NULL, 10, 450, 310, 450);
//    CGPathAddQuadCurveToPoint(curvedPath, NULL, 310, 10, 10, 10);
    //现在我们的路径,我们告诉动画我们想使用这条路径,那么我们发布的路径
    pathAnimation.path = path.CGPath;
//    CGPathRelease(curvedPath);
    
    UIImageView *circleView = [[UIImageView alloc] init];
    circleView.frame = CGRectMake(1, 1, 40, 40);
    circleView.layer.cornerRadius = 20;
    circleView.layer.masksToBounds = YES;
    circleView.backgroundColor = [UIColor redColor];
    [self addSubview:circleView];
    //添加动画circleView——一旦你添加动画层,动画开始
    [circleView.layer addAnimation:pathAnimation
                            forKey:@"moveTheSquare"];
    
}

@end
